package LiKou;

import java.util.Stack;

public class Test5 {
    public static void main(String[] args) {
        String s="(())(";
        System.out.println(longestValidParentheses(s));
    }
    public static int longestValidParentheses(String s){
        if(s.length()==0){
            return 0;
        }
        int result=0;
        for(int i=0;i<s.length();i++){
            int count=0;
            if(s.charAt(i)==')'){
                continue;
            }
            Stack<Character> stack=new Stack<>();
            for(int j=i;j<s.length();j++){
                if(s.charAt(j)==')' &&  stack.empty()){
                    if(count>result){
                        result=count;
                    }
                    break;
                }
                if(s.charAt(j)=='('){
                    stack.add('(');
                }
                if(s.charAt(j)==')'){
                    stack.pop();
                    count+=2;
                }
                if(j==s.length()-1){
                    if(count>result){
                        result=count;
                    }
                    break;
                }
            }
        }
        return result;
    }
}
